package cn.lg.soar.general.api.handler;

import cn.lg.soar.common.util.trans.handler.ITranslateHandler;
import cn.lg.soar.common.util.trans.model.TranslateContext;
import cn.lg.soar.general.api.service.IRegionApi;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.time.Duration;
import java.util.List;

/**
 * 文件上传接口
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Component
public class RegionTranslateHandler implements ITranslateHandler<Integer> {

    @Autowired
    private IRegionApi regionApi;

    private static final Cache<Integer, List<String>> CACHE = Caffeine.newBuilder()
            .initialCapacity(512)
            .maximumSize(4096)
            .expireAfterWrite(Duration.ofSeconds(120))
            .build();

    @Override
    public Object convertByJava(Integer javaValue, Annotation annotation, TranslateContext<Integer> context) {
        return CACHE.get(javaValue, regionApi::getNameList);
    }

    @Override
    public Integer convertToJava(Object value, Annotation annotation, TranslateContext<Integer> context) {
        return null;
    }

}
